# 基于Ubuntu 24.04
FROM ubuntu:24.04

# 避免交互提示
ENV DEBIAN_FRONTEND=noninteractive
# curl ssl
ENV CURL_SSL_NO_VERIFY=true

ARG UNAME=vscode
ARG UID=1001
ARG GID=1001

# Proxy
#ENV http_proxy="http://127.0.0.1:3128"
#ENV https_proxy="http://127.0.0.1:3128"
#ENV no_proxy="127.0.0.1,localhost,local,.local"

# 安装基础工具、交叉编译链、QEMU、mtools、ccache、GDB
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    locales tzdata sudo \
    # openssh-server curl wget \
    build-essential cmake git curl ca-certificates \
    gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
    qemu-user qemu-user-static qemu-system-arm qemu-system-aarch64 \
    binfmt-support mtools libguestfs-tools \
    ccache python3 python3-pip \
    gdb-multiarch \
    && rm -rf /var/lib/apt/lists/*

# 拷贝自签名证书
#COPY tools/usr/local/share/ca-certificates/SelfSign.crt /usr/local/share/ca-certificates/SelfSign.crt
#RUN update-ca-certificates

# source.list
# RUN mv /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

# 设置 UTF-8 本地化（可选但推荐）
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8

# 时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo 'Asia/Shanghai' > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata && \
    echo "export LC_ALL=en_US.UTF-8" >> /etc/profile && \
    echo "export LANG=en_US.UTF-8" >> /etc/profile

# 注册binfmt，让容器内能透明跑ARM64二进制（在宿主机预先挂载并注册:请在主机先执行scripts/register-binfmt.sh）
# RUN update-binfmts --enable qemu-aarch64

# 配置ccache，加速交叉编译
ENV CCACHE_DIR=/ccache \
    CC="ccache aarch64-linux-gnu-gcc" \
    CXX="ccache aarch64-linux-gnu-g++" \
    CCACHE_MAXSIZE=10G
RUN ccache -M ${CCACHE_MAXSIZE}

# 创建组并指定GID
RUN groupadd -g ${GID} ${UNAME}
# 创建vscode用户并指定UID、GID、默认shell和home目录，并添加sudo权限
RUN set -x && \
    useradd -m -u ${UID} -g ${GID} -s /bin/bash $UNAME && \
    echo "${UNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers 
    # echo 'vscode:123456' | chpasswd

# 设置工作目录权限（在切换用户之前）
RUN mkdir -p /workspace \
    && chown -R ${UID}:${GID} /workspace

# java cert
# RUN keytool -importcert -trustcacerts -noprompt -alias proxy_cert -file /usr/local/share/ca-certificates/SelfSign.crt -storepass changeit -keystore ${JAVA_HOME}/jre/lib/security/cacerts

# git
# RUN set -ex &&\
#     git config --global http.proxy http://127.0.0.1:3128 &&\
#     git config --global https.proxy http://127.0.0.1:3128 &&\
#     git config --global http.sslVerify false

# 设置默认用户
USER ${UNAME}

# ssh免密登录
#COPY tools/.ssh/id_rsa.pub ~/.ssh/id_rsa.pub
#COPY tools/.ssh/known_hosts ~/.ssh/known_hosts
#RUN mkdir -p ~/.ssh && \
#    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && \
#    chmod 700 ~/.ssh && \
#    chmod 600 ~/.ssh/authorized_keys && \
#     sed -i '/RSAAuthentication yes/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/PubkeyAuthentication yes/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/AuthorizedKeysFile/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/AllowAgentForwarding yes/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/AllowTcpForwarding yes/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/PasswordAuthentication yes/s/^#//' /etc/ssh/sshd_config && \
#     sed -i '/PermitRootLogin yes/s/^#//' /etc/ssh/sshd_config

## wget ssl
#RUN echo "check_certificate=off">>~/.wgetrc

# 下载并安装 lcov 2.3.2
RUN git clone https://github.com/linux-test-project/lcov.git /opt/lcov && \
    cd /opt/lcov && \
    git checkout v2.3.2 && \
    make install

# 安装所有 lcov 所需 Perl 模块(避免后续运行 lcov 或 genhtml 时出现其他缺失模块错误)
RUN apt-get update && \
    apt-get install -y perl \
    libcapture-tiny-perl \
    libdatetime-perl \
    libjson-xs-perl \
    libtext-csv-perl \
    libfile-slurp-perl \
    libfile-which-perl \
    libipc-system-simple-perl \
    libsort-naturally-perl \
    libdigest-md5-perl \
    libtimedate-perl

# 设置工作目录
WORKDIR /workspace

# 将ccache目录挂载点暴露给VSCode
VOLUME ["/ccache"]

# 将主目录内容复制到容器中
# COPY . .

# 创建构建目录并编译
# RUN mkdir -p build && cd build && cmake .. && make

# 构建项目并运行测试
# RUN mkdir -p build && cd build && \
#     cmake .. && \
#     make

# 生成覆盖率报告
# RUN mkdir -p test/build && cd test/build && \
#     cmake .. -DCMAKE_CXX_FLAGS="--coverage" -DCMAKE_C_FLAGS="--coverage" && \
#     make && \
#     ctest --output-on-failure  && \ 
#     lcov --gcov-tool /usr/bin/gcov --directory . --capture --output-file coverage.info --ignore-errors inconsistent && \
#     genhtml coverage.info --output-directory coverage-report

# 默认进入构建目录
# WORKDIR /app/build

# CMD ["/bin/bash"]